package client;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceInfo;
import javax.jmdns.ServiceListener;

public class ServiceManager implements ServiceListener {
	public static final String[] SERVICE_TYPE = { "AutoCheckIn._udp.local.", "MusicServer._tcp.local.", "FlightInfoServices._udp.local.", 
		"OfficeServices._udp.local.", "DutyFree._udp.local."};
	/**
	 * The List of Services resolved
	 * */
	private static ArrayList<Service> services;
	private List<ResolvedListener> listeners = new ArrayList<ResolvedListener>();
	JmDNS jmdns = null;

	public ServiceManager(){
		services = new ArrayList<Service>();
		try {
			jmdns = JmDNS.create();
			for (int i = 0; i < SERVICE_TYPE.length; i++) {
				jmdns.addServiceListener(SERVICE_TYPE[i], this);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void addListener(ResolvedListener listen){
		listeners.add(listen);
	}
	
	public void removeListener(ResolvedListener listen){
		listeners.remove(listen);
	}
	
	public void serviceAdded(ServiceEvent arg0) {
		System.out.println("Service added   : " + arg0.getName() + "." + arg0.getType());
		arg0.getDNS().requestServiceInfo(arg0.getType(), arg0.getName(), 0);
	}

	public void serviceRemoved(ServiceEvent arg0) {
		System.out.println("ServiceRemoved");
		System.out.println(arg0.getType());
		ServiceType t = null;
		if(arg0.getType().equals("MusicServer._tcp.local."))
			t = ServiceType.MusicServer;
		else if(arg0.getType().equals("OfficeServices._udp.local."))
			t = ServiceType.OfficeServices;
		else if(arg0.getType().equals("AutoCheckIn._udp.local."))
			t = ServiceType.AutoCheckIn;		
		else if(arg0.getType().equals("DutyFree._udp.local."))
			t = ServiceType.DutyFreeInfo;
		else if(arg0.getType().equals("FlightInfoServices._udp.local."))
			t = ServiceType.ArrivalsInfo;
		fireServiceRemovedEvent(arg0);
	}

	private void fireServiceRemovedEvent(ServiceEvent arg0) {
		System.out.println("Firing ServiceRemovedEvent");
        ServiceRemovedEvent sre = new ServiceRemovedEvent( this, arg0 );
        for (ResolvedListener list : listeners) {
	        list.ServiceRemoved(sre);
        }
	}

	public void serviceResolved(ServiceEvent arg0) {
		String my_serverHost = arg0.getInfo().getHostAddress();
		int my_serverPort = arg0.getInfo().getPort();
		ServiceType t = null;
		if(arg0.getType().equals("MusicServer._tcp.local."))
			t = ServiceType.MusicServer;
		else if(arg0.getType().equals("OfficeServices._udp.local."))
			t = ServiceType.OfficeServices;
		else if(arg0.getType().equals("AutoCheckIn._udp.local."))
			t = ServiceType.AutoCheckIn;		
		else if(arg0.getType().equals("DutyFree._udp.local."))
			t = ServiceType.DutyFreeInfo;
		else if(arg0.getType().equals("FlightInfoServices._udp.local."))
			t = ServiceType.ArrivalsInfo;
		Service s1 = new Service(arg0.getInfo().getName(), my_serverHost, my_serverPort, arg0.getInfo().getWeight(), arg0.getInfo().getPriority() ,t);
		services.add(s1);
		fireServiceResolvedEvent(s1);
	}
	
	public ArrayList<Service> getServices(){
		return services;
	}

    private synchronized void fireServiceResolvedEvent(Service s) {
    	ServiceResolvedEvent sre = new ServiceResolvedEvent( this, s );
        for (ResolvedListener list : listeners) {
	        list.ServiceResolved(sre);
        }
    }

}
